{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业要求\n",
    "    1. 取得一种数据集的数据\n",
    "    2. 计算其在各维度（列）的均值、方差\n",
    "    3. 计算各维之间的相关性（针对Continuous类型的列） 相关性计算，从pearson、spearman中选择一种。\n",
    "    4. 制作高斯核，尺寸取5 σ自取（至少带一位有效小数）\n",
    "## 交付内容：\n",
    "    1. 采用了哪个数据集，维度多少、记录数多少；\n",
    "    2. 各维度的均值、方差各是多少；\n",
    "    3. 维度及各自间的相关性数值（保留小数后2位）；\n",
    "    4. 高斯核的σ是多少，得到的高斯核是什么（保留小数后2位）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集维度: 32563 行 15 列\n",
      "记录数： 32563\n"
     ]
    }
   ],
   "source": [
    "# 读取数据集\n",
    "with open('./dataset/adult.data','r') as file:\n",
    "    # 读取文件内容\n",
    "    data = file.read()\n",
    "    # 将文件内容转换为列表,'\\n'作为分隔符\n",
    "    data = data.split('\\n')\n",
    "    # 计算数据集维度\n",
    "    rows = len(data)\n",
    "    cols = len(data[0].split(','))\n",
    "    print('数据集维度:', rows,'行', cols,'列')\n",
    "    # 计算记录数\n",
    "    records = rows\n",
    "    print('记录数：', records)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#计算各维度的均值和方差\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "      <th>14</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>39</td>\n",
       "      <td>State-gov</td>\n",
       "      <td>77516</td>\n",
       "      <td>Bachelors</td>\n",
       "      <td>13</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>Adm-clerical</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>2174</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>50</td>\n",
       "      <td>Self-emp-not-inc</td>\n",
       "      <td>83311</td>\n",
       "      <td>Bachelors</td>\n",
       "      <td>13</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Exec-managerial</td>\n",
       "      <td>Husband</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>38</td>\n",
       "      <td>Private</td>\n",
       "      <td>215646</td>\n",
       "      <td>HS-grad</td>\n",
       "      <td>9</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Handlers-cleaners</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>53</td>\n",
       "      <td>Private</td>\n",
       "      <td>234721</td>\n",
       "      <td>11th</td>\n",
       "      <td>7</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Handlers-cleaners</td>\n",
       "      <td>Husband</td>\n",
       "      <td>Black</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>28</td>\n",
       "      <td>Private</td>\n",
       "      <td>338409</td>\n",
       "      <td>Bachelors</td>\n",
       "      <td>13</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Prof-specialty</td>\n",
       "      <td>Wife</td>\n",
       "      <td>Black</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>Cuba</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0                  1       2           3   4                    5   \\\n",
       "0  39          State-gov   77516   Bachelors  13        Never-married   \n",
       "1  50   Self-emp-not-inc   83311   Bachelors  13   Married-civ-spouse   \n",
       "2  38            Private  215646     HS-grad   9             Divorced   \n",
       "3  53            Private  234721        11th   7   Married-civ-spouse   \n",
       "4  28            Private  338409   Bachelors  13   Married-civ-spouse   \n",
       "\n",
       "                   6               7       8        9     10  11  12  \\\n",
       "0        Adm-clerical   Not-in-family   White     Male  2174   0  40   \n",
       "1     Exec-managerial         Husband   White     Male     0   0  13   \n",
       "2   Handlers-cleaners   Not-in-family   White     Male     0   0  40   \n",
       "3   Handlers-cleaners         Husband   Black     Male     0   0  40   \n",
       "4      Prof-specialty            Wife   Black   Female     0   0  40   \n",
       "\n",
       "               13      14  \n",
       "0   United-States   <=50K  \n",
       "1   United-States   <=50K  \n",
       "2   United-States   <=50K  \n",
       "3   United-States   <=50K  \n",
       "4            Cuba   <=50K  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.read_csv('./dataset/adult.data',header=None)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         38.581647\n",
       "2     189778.366512\n",
       "4         10.080679\n",
       "10      1077.648844\n",
       "11        87.303830\n",
       "12        40.437456\n",
       "dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#每个维度的均值\n",
    "df.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     1.860614e+02\n",
       "2     1.114080e+10\n",
       "4     6.618890e+00\n",
       "10    5.454254e+07\n",
       "11    1.623769e+05\n",
       "12    1.524590e+02\n",
       "dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#每个维度的方差\n",
    "df.var()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>2</th>\n",
       "      <th>4</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.076646</td>\n",
       "      <td>0.036527</td>\n",
       "      <td>0.077674</td>\n",
       "      <td>0.057775</td>\n",
       "      <td>0.068756</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.076646</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.043195</td>\n",
       "      <td>0.000432</td>\n",
       "      <td>-0.010252</td>\n",
       "      <td>-0.018768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.036527</td>\n",
       "      <td>-0.043195</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.122630</td>\n",
       "      <td>0.079923</td>\n",
       "      <td>0.148123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.077674</td>\n",
       "      <td>0.000432</td>\n",
       "      <td>0.122630</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.031615</td>\n",
       "      <td>0.078409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.057775</td>\n",
       "      <td>-0.010252</td>\n",
       "      <td>0.079923</td>\n",
       "      <td>-0.031615</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.054256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.068756</td>\n",
       "      <td>-0.018768</td>\n",
       "      <td>0.148123</td>\n",
       "      <td>0.078409</td>\n",
       "      <td>0.054256</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         2         4         10        11        12\n",
       "0   1.000000 -0.076646  0.036527  0.077674  0.057775  0.068756\n",
       "2  -0.076646  1.000000 -0.043195  0.000432 -0.010252 -0.018768\n",
       "4   0.036527 -0.043195  1.000000  0.122630  0.079923  0.148123\n",
       "10  0.077674  0.000432  0.122630  1.000000 -0.031615  0.078409\n",
       "11  0.057775 -0.010252  0.079923 -0.031615  1.000000  0.054256\n",
       "12  0.068756 -0.018768  0.148123  0.078409  0.054256  1.000000"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#各维度的相关系数\n",
    "df.corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.00296902 0.01330621 0.02193823 0.01330621 0.00296902]\n",
      " [0.01330621 0.0596343  0.09832033 0.0596343  0.01330621]\n",
      " [0.02193823 0.09832033 0.16210282 0.09832033 0.02193823]\n",
      " [0.01330621 0.0596343  0.09832033 0.0596343  0.01330621]\n",
      " [0.00296902 0.01330621 0.02193823 0.01330621 0.00296902]]\n"
     ]
    }
   ],
   "source": [
    "#制作高斯核，尺寸为5，标准差1.0\n",
    "import math\n",
    "\n",
    "def gaussian_kernel(size, sigma):\n",
    "    kernel = np.zeros((size, size))\n",
    "    center = size // 2\n",
    "\n",
    "    for i in range(size):\n",
    "        for j in range(size):\n",
    "            x = i - center\n",
    "            y = j - center\n",
    "            kernel[i][j] = math.exp(-(x**2 + y**2) / (2 * sigma**2))\n",
    "    \n",
    "    kernel = kernel / np.sum(kernel)\n",
    "\n",
    "    return kernel\n",
    "\n",
    "size = 5\n",
    "sigma = 1.0\n",
    "\n",
    "kernel = gaussian_kernel(size, sigma)\n",
    "\n",
    "print(kernel)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
